import Vue from 'vue';
import VueRouter from 'vue-router';

Vue.use(VueRouter);
//引入路由
import Home from '@/views/Home';
import Search from '@/views/Search';
import Register from '@/views/Register';
import Login from '@/views/Login';

let originPush = VueRouter.prototype.push;
let originReplace = VueRouter.prototype.replace;
//重写push | replaces
//第一个参数：告诉原来push方法，往哪里跳转（传递哪些参数）
VueRouter.prototype.push = function(location,reslove,reject) {
    if(reslove && reject) {
        originPush.call(this,location,reslove,reject);
    }else {
        originPush.call(this,location,() => {},() => {});
    }
}

VueRouter.prototype.replace = function(location,resolve,reject) {
    if(resolve && reject) {
        originReplace.call(this,location,resolve,reject);
    } else {
        originReplace.call(this,location,() => {},() => {});
    }
}

export default new VueRouter({
    routes:[
        {
            path: "/home",
            component: Home,
            meta: {show: true}
        },
        {
            path: "/search/:keyword?",
            component: Search,
            meta: {show: true},
            name: "search",
            props: ($route) => ({keyword: $route.params.keyword,k:$route.query.k})
        },
        {
            path: "/register",
            component: Register,
            meta: {show: false}
        },
        {
            path: "/login",
            component: Login,
            meta: {show: false}
        },
        //重定向,项目跑起来时，访问/，立马定向到是首页
        {
            path: '/',
            redirect: '/home'
        }
    ]
})